var game_config = require("../game_config.js");
var express = require("express");
var path = require("path");
var fs = require("fs");
const https = require('https');
var util = require("util");


var log = require("../../utils/log.js");
var Cmd = require("../Cmd.js");
var Stype = require("../Stype.js");
var utils = require("../../utils/utils.js");
/*
if (process.argv.length < 3) {
	console.log("node webserver.js port");
	return;
}
*/
var app = express();
//设置跨域访问
app.all('*', function(req, res, next) {
	res.header("Access-Control-Allow-Origin", "*");
	res.header("Access-Control-Allow-Headers", "X-Requested-With");
	res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
	res.header("X-Powered-By",' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
	next();
});
// end

var host = game_config.webserver.host;
var port = game_config.webserver.port;

// process.chdir("./apps/webserver");
// console.log(process.cwd());

if (fs.existsSync("www_root")) {
	app.use(express.static(path.join(process.cwd(), "www_root")));	
}
else {
	log.warn("www_root is not exists!!!!!!!!!!!");
}



log.info("webserver started at port ", host, 80);

// 获取客户端连接的服务器信息, 
// http://127.0.0.1:10001/server_info
app.get("/wx", function (request, respones) {
	var data = request.query;
	if (!data.signature || !data.echostr || 
		!data.timestamp || !data.nonce) {
		return;
	}

	var token = "bycw";
	var list = [token, data.timestamp, data.nonce];
	list.sort();
	var hashcode = utils.sha1(list[0] + list[1] + list[2]);
	log.info("data.signature:", data.signature);
	if (hashcode == data.signature) {
		respones.send(data.echostr);
	} 
});

var get_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
var bycw_appid = "wxadbfa1e5cb1549f3";
var app_pwd = "da8d2b8ba78fe5c1f62938c1e331795f";
var	get_uinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN"
function get_user_info(access_token, openid) {
	// console.log(access_token);
	// console.log(openid);

	var uinfo_url = util.format(get_uinfo_url, access_token, openid);
	https.get(uinfo_url, function (res) {
		var data_str = "";
		res.on("data", function (data) {
			data_str += data.toString("utf8");
		});

		res.on("end", function () {
			var body = JSON.parse(data_str);
			if (body.errcode) {
				return;
			}

			if(body) {
				body.headimgurl = decodeURIComponent(body.headimgurl);
				console.log("=======User Info:=========");
				console.log(body.openid);
				console.log(body.nickname);
				console.log(body.sex);
				console.log(body.headimgurl);
				console.log("================");
			}
		});
	});
}

app.get("/wx_code", function(request, respones) {
	console.log("##########https");
	
	var code = request.query.code;
	// 获取access_token
	var access_token_url = util.format(get_access_token_url, bycw_appid, app_pwd, code);
	https.get(access_token_url, function (res) {
		var data_str = "";
		res.on("data", function (data) {
			data_str += data.toString("utf8")
		});

		res.on("end", function () {
			var body = JSON.parse(data_str);
			if (body.errcode) { // 有错误
				// console.log(data_str);
				return;
			}

			// console.log(data_str);

			get_user_info(body.access_token, body.openid);
			respones.send("SUCCESS");
		});
	});
});

app.listen(80);


//test001